#include "lcd.h"
#include "platform.h"

/**
  * @brief  write a byte
  * @param  data: data to write
  * @retval return data
  */
uint8_t spi_byte_write(uint8_t data)
{
    uint8_t brxbuff;
//    spi_i2s_dma_transmitter_enable(SPI3, FALSE);
//    spi_i2s_dma_receiver_enable(SPI3, FALSE);
    spi_i2s_data_transmit(SPI3, data);
    while(spi_i2s_flag_get(SPI3, SPI_I2S_RDBF_FLAG) == RESET);
    brxbuff = spi_i2s_data_receive(SPI3);
    while(spi_i2s_flag_get(SPI3, SPI_I2S_BF_FLAG) != RESET);
    return brxbuff;
}

void LCD_WR_DATA8(uint8_t dat)
{
    spi_byte_write(dat);
}

void LCD_WR_DATA(uint16_t dat)
{
    LCD_WR_DATA8(dat>>8);
    LCD_WR_DATA8(dat);
}

void LCD_WR_REG(uint8_t dat)
{
    LCD_DC_L();//写命令
    LCD_WR_DATA8(dat);
    LCD_DC_H();//写数据
}

void LCD_Address_Set(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2)
{
    LCD_WR_REG(0x2a);//列地址设置
    LCD_WR_DATA(x1);
    LCD_WR_DATA(x2);
    LCD_WR_REG(0x2b);//行地址设置
    LCD_WR_DATA(y1);
    LCD_WR_DATA(y2);
    LCD_WR_REG(0x2c);//储存器写
}

void LCD_Init(void)
{
    LCD_RES_L();//复位
    delay_ms(30);
    LCD_RES_H();
    delay_ms(120);
    //LCD_BL_EN();//打开背光
    delay_ms(100);
    LCD_WR_REG(0x11);
    delay_ms(120); 
    LCD_WR_REG(0x36);
    
    if(USE_HORIZONTAL==0)LCD_WR_DATA8(0x00);
    else if(USE_HORIZONTAL==1)LCD_WR_DATA8(0xC0);
    else if(USE_HORIZONTAL==2)LCD_WR_DATA8(0x70);
    else LCD_WR_DATA8(0xA0);

    LCD_WR_REG(0x3A);
    LCD_WR_DATA8( 0x05);   //16BIT

    LCD_WR_REG(0xB2);
    LCD_WR_DATA8( 0x05);
    LCD_WR_DATA8( 0x05);
    LCD_WR_DATA8( 0x00);
    LCD_WR_DATA8( 0x33);
    LCD_WR_DATA8( 0x33);

    LCD_WR_REG(0xB7);
    LCD_WR_DATA8( 0x35);


    LCD_WR_REG(0xBB);
    LCD_WR_DATA8( 0x21);

    LCD_WR_REG(0xC0);
    LCD_WR_DATA8( 0x2C);

    LCD_WR_REG(0xC2);
    LCD_WR_DATA8( 0x01);

    LCD_WR_REG(0xC3);
    LCD_WR_DATA8( 0x0B);

    LCD_WR_REG(0xC4);
    LCD_WR_DATA8( 0x20);

    LCD_WR_REG(0xC6);
    LCD_WR_DATA8( 0x0F);   //60HZ dot inversion

    LCD_WR_REG(0xD0);
    LCD_WR_DATA8( 0xA7);
    LCD_WR_DATA8( 0xA1);

    LCD_WR_REG(0xD0);
    LCD_WR_DATA8( 0xA4);
    LCD_WR_DATA8( 0xA1);


    LCD_WR_REG(0xD6);
    LCD_WR_DATA8( 0xA1);

    LCD_WR_REG(0xE0);
    LCD_WR_DATA8( 0xD0);
    LCD_WR_DATA8( 0x04);
    LCD_WR_DATA8( 0x08);
    LCD_WR_DATA8( 0x0A);
    LCD_WR_DATA8( 0x09);
    LCD_WR_DATA8( 0x05);
    LCD_WR_DATA8( 0x2D);
    LCD_WR_DATA8( 0x43);
    LCD_WR_DATA8( 0x49);
    LCD_WR_DATA8( 0x09);
    LCD_WR_DATA8( 0x16);
    LCD_WR_DATA8( 0x15);
    LCD_WR_DATA8( 0x26);
    LCD_WR_DATA8( 0x2B);

    LCD_WR_REG(0xE1);
    LCD_WR_DATA8( 0xD0);
    LCD_WR_DATA8( 0x03);
    LCD_WR_DATA8( 0x09);
    LCD_WR_DATA8( 0x0A);
    LCD_WR_DATA8( 0x0A);
    LCD_WR_DATA8( 0x06);
    LCD_WR_DATA8( 0x2E);
    LCD_WR_DATA8( 0x44);
    LCD_WR_DATA8( 0x40);
    LCD_WR_DATA8( 0x3A);
    LCD_WR_DATA8( 0x15);
    LCD_WR_DATA8( 0x15);
    LCD_WR_DATA8( 0x26);
    LCD_WR_DATA8( 0x2A);

    LCD_WR_REG(0x21);

    LCD_WR_REG(0x29);
}

void LCD_Fill(uint16_t xsta,uint16_t ysta,uint16_t xend,uint16_t yend,uint16_t color)
{
    uint16_t i,j;
    LCD_Address_Set(xsta,ysta,xend-1,yend-1);//设置显示范围
    for(i=ysta;i<yend;i++)
    {
        for(j=xsta;j<xend;j++)
        {
            LCD_WR_DATA(color);
        }
    }
}

void LCD_DrawPoint(uint16_t x,uint16_t y,uint16_t color)
{
    LCD_Address_Set(x,y,x,y);//设置光标位置 
    LCD_WR_DATA(color);
} 

void LCD_DrawLine(uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,uint16_t color)
{
    uint16_t t; 
    int xerr=0,yerr=0,delta_x,delta_y,distance;
    int incx,incy,uRow,uCol;
    delta_x=x2-x1; //计算坐标增量 
    delta_y=y2-y1;
    uRow=x1;//画线起点坐标
    uCol=y1;
    if(delta_x>0)incx=1; //设置单步方向 
    else if (delta_x==0)incx=0;//垂直线 
    else {incx=-1;delta_x=-delta_x;}
    if(delta_y>0)incy=1;
    else if (delta_y==0)incy=0;//水平线 
    else {incy=-1;delta_y=-delta_y;}
    if(delta_x>delta_y)distance=delta_x; //选取基本增量坐标轴 
    else distance=delta_y;
    for(t=0;t<distance+1;t++)
    {
        LCD_DrawPoint(uRow,uCol,color);//画点
        xerr+=delta_x;
        yerr+=delta_y;
        if(xerr>distance)
        {
            xerr-=distance;
            uRow+=incx;
        }
        if(yerr>distance)
        {
            yerr-=distance;
            uCol+=incy;
        }
    }
}

void LCD_DrawRectangle(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2,uint16_t color)
{
    LCD_DrawLine(x1,y1,x2,y1,color);
    LCD_DrawLine(x1,y1,x1,y2,color);
    LCD_DrawLine(x1,y2,x2,y2,color);
    LCD_DrawLine(x2,y1,x2,y2,color);
}

void Draw_Circle(uint16_t x0,uint16_t y0,uint8_t r,uint16_t color)
{
    int a,b;
    a=0;b=r;
    while(a<=b)
    {
        LCD_DrawPoint(x0-b,y0-a,color);             //3
        LCD_DrawPoint(x0+b,y0-a,color);             //0
        LCD_DrawPoint(x0-a,y0+b,color);             //1
        LCD_DrawPoint(x0-a,y0-b,color);             //2
        LCD_DrawPoint(x0+b,y0+a,color);             //4
        LCD_DrawPoint(x0+a,y0-b,color);             //5
        LCD_DrawPoint(x0+a,y0+b,color);             //6 
        LCD_DrawPoint(x0-b,y0+a,color);             //7
        a++;
        if((a*a+b*b)>(r*r))//判断要画的点是否过远
        {
            b--;
        }
    }
}
